package com.auto.autoapp.ui.task

import android.content.Intent
import android.provider.Settings
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.auto.autoapp.R
import com.auto.autoapp.adapter.TaskAdapter
import com.auto.autoapp.base.BaseFragment
import com.auto.autoapp.databinding.FragmentTaskBinding
import com.auto.autoapp.model.TaskInfo
import com.auto.autoapp.ui.activity.AccountRaiseSettingActivity
import com.auto.autoapp.ui.activity.CommentPrivateMessageActivity
import com.auto.autoapp.ui.activity.LiveMemberPrivateMsgActivity
import com.auto.autoapp.ui.activity.UpVideoListenActivity
import com.auto.autoapp.ui.activity.WatchVideoCommentActivity
import com.auto.autoapp.widgets.SimpleToolbar
import com.ven.assists.AssistsCore
import androidx.core.net.toUri

class TaskFragment : BaseFragment() {

    companion object Companion {
        fun newInstance(): TaskFragment {
            return TaskFragment()
        }
    }

    private lateinit var _binding: FragmentTaskBinding
    private val taskAdapter = TaskAdapter()
    override fun onInit() {
        _binding.rvTask.apply {
            layoutManager = LinearLayoutManager(requireContext())
            adapter = taskAdapter
        }

        val tasks = listOf(
            TaskInfo(
                iconRes = R.drawable.ic_task, name = "养号",
                AccountRaiseSettingActivity::class.java,
                subTitle = "活跃账号，执行浏览视频，关注作者，点赞视频等"
            ),
            TaskInfo(
                iconRes = R.drawable.ic_task,
                name = "搜索视频评论区意向客户私信",
                CommentPrivateMessageActivity::class.java,
                subTitle = "搜索关键字查找到相关视频，对视频评论区意向客户进行私信"
            ),
            TaskInfo(
                iconRes = R.drawable.ic_task,
                name = "直播间成员私信",
                LiveMemberPrivateMsgActivity::class.java,
                subTitle = "进入直播间进行成员留言监听，并对留言者进行私信"
            ),
            TaskInfo(
                iconRes = R.drawable.ic_task,
                name = "主播新视频监听并评论",
                UpVideoListenActivity::class.java,
                subTitle = "对指定的主播进行监听，发现有新作品立即进行评论等相关操作"
            ),
            TaskInfo(
                iconRes = R.drawable.ic_task,
                name = "视频新评论监听并私信",
                WatchVideoCommentActivity::class.java,
                subTitle = "对指定视频进行监听，发现有新留言立即进行相关操作"
            )
        )
        taskAdapter.setList(tasks)
        taskAdapter.setOnItemClickListener { _, _, position ->

            if (!AssistsCore.isAccessibilityServiceEnabled()) {
                AssistsCore.openAccessibilitySetting()
                return@setOnItemClickListener
            }

            if (!Settings.canDrawOverlays(requireContext())) {
                val intent = Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION)
                intent.data = "package:${requireContext().packageName}".toUri()
                startActivityForResult(intent, 1002)
                return@setOnItemClickListener
            }

            startActivity(Intent(requireContext(), taskAdapter.getItem(position).clazz).apply {
                putExtra("title", taskAdapter.getItem(position).name)
            })
        }
    }

    override fun provideView(): View {
        _binding = FragmentTaskBinding.inflate(layoutInflater)
        return _binding.root
    }

    override fun configToolbar(toolbar: SimpleToolbar) {
        toolbar.setTitle("任务")
        toolbar.hideBack()
    }

}